package com.bbs.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.bbs.comm.SessionContext;
import com.bbs.dao.ReplyDao;
import com.bbs.dao.impl.ReplyDaoImpl;
import com.bbs.model.Reply;
import com.bbs.model.User;
import com.bbs.util.PageSplitUtil;

public class ReplyAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Reply reply;
	private List<Reply> replyList;
	private PageSplitUtil psu;
	
	public String addReply() throws IOException{
		reply.setReply_time
			(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		ReplyDao replyDao=new ReplyDaoImpl();
		addPrefix(reply);
		replyDao.save(reply);
		return "addReply";
	}
	
	//对不是主贴的回复加上前缀
	public void addPrefix(Reply reply){
		if(0!=reply.getReply_parent_id()){
			String prefix;
			String fromUser=SessionContext.getSessionContext().getUser().getUser_name();
			String toUser=((Map<Integer, User>) ServletActionContext.getServletContext().getAttribute("userCache")).
							get(reply.getReply_to_user_id()).getUser_name();
			prefix="[<strong>"+fromUser+"对"+toUser+"回复]</strong>";
			reply.setReply_content(prefix+reply.getReply_content());
		}
	}
	
	public String getReplyListById() throws IOException{
		int id=Integer.parseInt(getRequest().getParameter("id"));
		
		setReplyList(new ReplyDaoImpl().getReplyList(id));
		return "getReplyListById";
	}
	
	/*
	 *分页浏览 
	 */
	public String pageBrowse() throws IOException{
		int id=Integer.parseInt(getRequest().getParameter("id"));
		
		replyList=new ReplyDaoImpl().pageBrowse(psu,id);
		return "pageBrowse";
	}
	
	public Reply getReply() {
		return reply;
	}
	public void setReply(Reply reply) {
		this.reply = reply;
	}
	public List<Reply> getReplyList() {
		return replyList;
	}
	public void setReplyList(List<Reply> replyList) {
		this.replyList = replyList;
	}
	public PageSplitUtil getPsu() {
		return psu;
	}
	public void setPsu(PageSplitUtil psu) {
		this.psu = psu;
	}

}
